package com.android.matmanual;

import java.io.File;

import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.StatFs;
import android.text.format.Formatter;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;



public class SdcardTest extends BaseActivity {

	TextView mRecordingText;

	WakeLock mWakeLock;

	Dev_MountInfo dev = null;
	DevInfo info = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.sdcard_test);
		setTitle(R.string.sdcard);
		PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
				"XYTEST");
		mWakeLock.acquire();
		mRecordingText = (TextView) findViewById(R.id.recording_text);
		if (!isSdPresent()) {
			mRecordingText.setText(R.string.sdcard_not_exist);
			mRecordingText.setTextColor(0xffff0000);
			mRecordingText.setVisibility(View.VISIBLE);
		} else {
			StringBuilder sb = new StringBuilder();
						
			sb.append("已经成功检测到SD卡：\n"); 
			sb.append("当前SD卡大小是：		");
			sb.append(getSDTotalSize());
			//mRecordingText.setText(R.string.sdcard_exist);
			mRecordingText.setText(sb.toString());
			mRecordingText.setTextColor(0xffffffff);
			mRecordingText.setVisibility(View.VISIBLE);

		}
	}

	private String getSDTotalSize() {  
        File path = Environment.getExternalStorageDirectory();  
        StatFs stat = new StatFs(path.getPath());  
        long blockSize = stat.getBlockSize();  
        long totalBlocks = stat.getBlockCount();  
        return Formatter.formatFileSize(this, blockSize * totalBlocks);  
    }  

	public static class EnvironmentShare {

		public static boolean haveSdCard() {
			return Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED);
		}

	}

	/*
	 * Checks whether the SD Card is present or not
	 */
	private boolean isSdPresent() {

		File file;
		if (FeatureSwitcher.isSdCardSupport()) {
			file = new File("/storage/sdcard1");
		} else {
			file = new File("/storage/sdcard0");
		}

		if (file.exists() && file.isDirectory()) {
			
			if (file != null && file.list() != null) {
				
				return true;
			}

		}

		return false;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mWakeLock != null) {
			mWakeLock.release();
			mWakeLock = null;
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		finish();
		super.onStop();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
		}
		return true;
	}

	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}

}

